<template>
    <a-modal v-model:visible="visible" :title="title" :mask-closable="false" :esc-to-close="false" draggable
        :width="width >= 600 ? 600 : '100%'" @before-ok="save" @close="reset">
        <GiForm ref="formRef" v-model="form" :options="options" :columns="columns" />
    </a-modal>
</template>

<script setup lang="ts">
import { Message } from '@arco-design/web-vue'
import { useWindowSize } from '@vueuse/core'
import { useResetReactive } from '@/hooks'
import { useDict } from '@/hooks/app'
import { type Columns, GiForm, type Options } from '@/components/GiForm'
import { addInfo,getInfo,updateInfo } from '@/apis/task/info'
const emit = defineEmits<{
    (e: 'save-success'): void
}>()

const { width } = useWindowSize()

const dataId = ref('')
const visible = ref(false)
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改测试任务' : '新增测试任务'))
const formRef = ref<InstanceType<typeof GiForm>>()
const { task_status } = useDict('task_status')
const options: Options = {
    form: { size: 'large' },
    btns: { hide: true },
}

const [form, resetForm] = useResetReactive({
    // todo 待补充
})

const columns: Columns = reactive([
    {
        label: '任务名称',
        field: 'name',
        type: 'input',
        rules: [{ required: true, message: '请输入任务名称' }]
    },
    {
        label: '备注',
        field: 'mark',
        type: 'input',
    },
    {
        label: '项目ID',
        field: 'projectId',
        type: 'input',
    },
    {
        label: '设备ID',
        field: 'deviceId',
        type: 'input',
    },
    {
        label: '责任人',
        field: 'dutyUser',
        type: 'input',
    },
    {
        label: '参与人',
        field: 'participant',
        type: 'input',
    },
    {
        label: '计划开始时间',
        field: 'planStartTime',
        type: 'date-picker',
        rules: [{ required: true, message: '请输入计划开始时间' }],
        props: {
            showTime: true,
        },
    },
    {
        label: '计划结束时间',
        field: 'planEndTime',
        type: 'date-picker',
        rules: [{ required: true, message: '请输入计划结束时间' }],
        props: {
            showTime: true,
        },
    },
    {
        label: '实际开始时间',
        field: 'actualStartTime',
        type: 'date-picker',
        rules: [{ required: true, message: '请输入实际开始时间' }],
        props: {
            showTime: true,
        },
    },
    {
        label: '实际结束时间',
        field: 'actualEndTime',
        type: 'date-picker',
        rules: [{ required: true, message: '请输入实际结束时间' }],
        props: {
            showTime: true,
        },
    },
    {
        label: '状态',
        field: 'status',
        type: 'select',
        options: task_status,
        rules: [{ required: true, message: '请输入状态' }]
    },
    {
        label: '文件名称',
        field: 'fileName',
        type: 'input',
    },
    {
        label: '文件过程数据记录',
        field: 'fileDataRecord',
        type: 'input',
    },
])

// 重置
const reset = () => {
    formRef.value?.formRef?.resetFields()
    resetForm()
}

// 保存
const save = async () => {
    try {
        const isInvalid = await formRef.value?.formRef?.validate()
        if (isInvalid) return false
        if (isUpdate.value) {
            await updateInfo(form, dataId.value)
            Message.success('修改成功')
        } else {
            await addInfo(form)
            Message.success('新增成功')
        }
        emit('save-success')
        return true
    } catch (error) {
        return false
    }
}

// 新增
const onAdd = async () => {
    reset()
    dataId.value = ''
    visible.value = true
}

// 修改
const onUpdate = async (id: string) => {
    reset()
    dataId.value = id
    const { data } = await getInfo(id)
    Object.assign(form, data)
    visible.value = true
}

defineExpose({ onAdd, onUpdate })
</script>

<style scoped lang="scss"></style>